#pragma once

#include <QSslSocket>
#include <assert.h>
#include <unordered_map>
#include <functional>

namespace TCServer
{
    class Cpu
    {
    public:
        // Typedefs
        using TaskProcessFunctionType = std::function<void(QSslSocket *, QVariantMap args)>;
    public:
        Cpu();
        ~Cpu();
    public:
        template <class T>
        void setOn(const QString &type, T *thisptr, void (T::* func)(QSslSocket *, QVariantMap));
        void setOn(const QString &type, TaskProcessFunctionType func);
    public:
        void onMessage(QSslSocket *s, const QString &type, const QVariantMap &args);
    public:
        // public static methods:
        static Cpu *instance();
    private:
        // internal static variables
        static Cpu *_instance;
    private:
        std::unordered_map<std::string, TaskProcessFunctionType> tasks = std::unordered_map<std::string, TaskProcessFunctionType>(1024);
    };

    template <class T>
    inline void Cpu::setOn(const QString &type, T *thisptr, void (T::* func)(QSslSocket *, QVariantMap))
    {
        return setOn(type, std::bind(func, thisptr, std::placeholders::_1, std::placeholders::_2));
    }
}